package Servidor;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author usuario
 */
public class ServidorThread extends Thread{
    private int numThread=0;
    private boolean fin=false;
    private AdminPartidas ADMON;
    private ObjectOutputStream oos = null;
    private ObjectInputStream ois = null;
    private Socket socket=null;
    private String nombre=null;

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getNumThread() {
        return numThread;
    }

    public void setNumThread(int numThread) {
        this.numThread = numThread;
    }



    public ServidorThread(Socket socket, AdminPartidas M) {
            super("ServidorThread");
            this.socket = socket;
            try {
                oos = new ObjectOutputStream(socket.getOutputStream());
                ois = new ObjectInputStream(socket.getInputStream());
            } catch (IOException ex) {
                Logger.getLogger(ServidorThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            ADMON=M;   
    }
    
    @Override
    @SuppressWarnings("empty-statement")
    public void run(){
        try {

                while(!fin){
                    SerializedObject os1=null;
                    os1=this.recibir();
                    String temp=null;
                    //System.out.println(mensaje);

                
                    switch(Integer.parseInt(os1.getTipoM())){
                        case -1:break;
                        case 0: fin=true;
                                this.close();
                                break;
                        case 1: temp=ADMON.getPartidas()+"";
                                os1.setMensaje(temp);
                                this.enviar(os1);
                                break;
                        case 2: temp=ADMON.selectPartidas(Integer.parseInt(os1.getMensaje()),this.numThread);
                                this.setNombre(os1.getName());
                                os1.setMensaje(temp);
                                this.enviar(os1);
                                
                                fin=true;
                                break;
                        case 3: 
                                break;

                        case 5: break;
                    }   
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
    }

        public void enviar(SerializedObject so){
            try{
                // write the objects to the server
                oos.writeObject(so);
                oos.flush();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public SerializedObject recibir(){
                SerializedObject result = null;
            try{
                // read an object from the server
                result = (SerializedObject) ois.readObject();
            }catch(Exception e){
                e.printStackTrace();
            }
            return result;
        }

        public void close(){
           try {
                oos.close();
                ois.close();
                socket.close();
           }catch(Exception e){
                System.out.println(e.getMessage());
           }
        }

}
